Skip to main content

JavaScript heap out of memory

How to solve JavaScript error “Heap out of memory”?

When we create objects, arrays, and other dynamically-allocated data types in JavaScript, they're stored in the heap. The 'Heap out of memory' error arises when the JavaScript engine, such as V8 in Node.js or browsers, cannot allocate enough memory space in the heap for an operation.

On Windows

Open PowerShell.

Run the following command to set larger memory limit:

$env:NODE_OPTIONS="--max-old-space-size=6144"

Setting this command in PowerShell on Windows would allocate a maximum of 4 gigabytes (6144 megabytes) of memory for the Node.js process.

NODE_OPTIONS is a special environment variable recognized by Node.js. It allows you to pass command-line options to the Node.js process.

--max-old-space-size=6144 sets the maximum heap size to 6144 megabytes (6 gigabytes).

If you only want to increase the heap memory temporarily, run the below command in a PowerShell terminal before running your project:

set NODE_OPTIONS=--max-old-space-size=6144

You can check the memory allocated by using the command :

node -e 'console.log(v8.getHeapStatistics().heap_size_limit/(1024*1024))'

On MacOS / Linux

Open terminal.

Run the following command to set larger memory limit:

export NODE_OPTIONS=--max-old-space-size=12000